﻿
using MailChatSms;
using System;

namespace StockTrader.Platform.Logging
{
    [Flags]
    public enum MessagingChannel
    {
        EMAIL = 1,
        CHAT = 2,
        SMS = 4
    }
    public static class MessagingUtils
    {
        static string _gUser = "@gmail.com";
        static string _gPassword = "";
        static string _mailTo = "@gmail.com";//, 
        static string _chatTo = "@gmail.com";//, 

        static string _sUser1 = "995445422490";
        static string _sPassword1 = "1982";
        static string _sUser2 = "939545454521782";
        static string _sPassword2 = "1982";
        static string _sUser3 = "9920323232366";
        static string _sPassword3 = "";
        static string _smsTo = "992012333";

        static Smtp GmailSender;
        static Xmpp Gtalk;
        static Imap GmailReader;
        static Way2SmsAPI Sms;
        static int smsAccountNum = 1;

        const int SINGLE_SMS_MAX_LENGTH = 125;
        const int SINGLEACCOUNT_SMS_MAX_NUM = 99;
        const string SMS_HEADER_FORMAT = "({0} {1}/{2}) {3}"; // (subject part/total), body

        public static void Init(Imap.OnImapMessageReceive _handlerImap, Xmpp.OnXmppMessageReceive _handlerXmpp, string acceptMsgFrom = "")
        {
            Gtalk = new Xmpp(_gUser, _gPassword, _handlerXmpp, acceptMsgFrom);
            //GmailReader = new Imap(_gUser, _gPassword, _handlerImap, acceptMsgFrom);
            GmailSender = new Smtp("smtp.gmail.com", 587, _gUser, _gPassword, true);
            Sms = new Way2SmsAPI(_sUser1, _sPassword1);
            Gtalk.Start();
        }

        public static void ReadMailMessages()
        {
            //BUGBUG TODO EAGetEmail has expired
            //GmailReader.ReadEmails();
        }

        public static void SendAlertMessage(string subject, string body, string smsBody = null,
            MessagingChannel channel = MessagingChannel.EMAIL | MessagingChannel.SMS | MessagingChannel.CHAT)
        {
            try
            {
                body = string.Format("{0} at {1}", body, DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString());
                
                if (channel.HasFlag(MessagingChannel.EMAIL))
                    GmailSender.SendMail(_gUser, _mailTo, subject, body, false, 3);

                if (channel.HasFlag(MessagingChannel.CHAT))
                    Gtalk.Send(_chatTo, subject + "-----" + body);

                if (smsBody == null)
                    smsBody = body;
                else
                    smsBody = string.Format("{0} at {1}", smsBody, DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString());

                if (channel.HasFlag(MessagingChannel.SMS) && smsBody.Length < 700)
                {
                    string remainingBody = smsBody;
                    int totalParts = remainingBody.Length/SINGLE_SMS_MAX_LENGTH + 1;
                    int i = 1;
                    string msg;

                    if (Sms.NumSmsSent > SINGLEACCOUNT_SMS_MAX_NUM)
                    {
                        smsAccountNum++;
                        if (smsAccountNum == 2)
                        {
                            Sms = new Way2SmsAPI(_sUser2, _sPassword2);
                        }
                        else if (smsAccountNum == 3)
                        {
                            Sms = new Way2SmsAPI(_sUser3, _sPassword3);
                        }
                    }

                    // Send in multiple msgs if exceeding single message limit
                    while (remainingBody.Length > SINGLE_SMS_MAX_LENGTH)
                    {
                        var part = remainingBody.Substring(0, SINGLE_SMS_MAX_LENGTH);
                        msg = string.Format(SMS_HEADER_FORMAT, subject, i++, totalParts, part);
                        Sms.SendSms(_smsTo, msg, false);
                        remainingBody = remainingBody.Substring(SINGLE_SMS_MAX_LENGTH);
                    }
                    msg = string.Format(SMS_HEADER_FORMAT, subject, i++, totalParts, remainingBody);
                    Sms.SendSms(_smsTo, msg);
                }
            }
            catch (Exception ex)
            {
                Logger.LogException(ex);
            }
        }

    }
}

//int extraParts = ((totalParts + 1) * (subject.Length + 7)) / SINGLE_SMS_MAX_LENGTH;

//int totalLen = totalParts * SINGLE_SMS_MAX_LENGTH - remainingBody.Length;
//totalLen = 

//extraParts += (remainingBody.Length + (totalParts * (subject.Length + 7))) / SINGLE_SMS_MAX_LENGTH;
//totalParts = extraParts += extraParts + 1;

//while (extraParts-- > 0)
//int len = SINGLE_SMS_MAX_LENGTH - subject.Length - 7;
//